package com.oocl.view;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.constant.LogLevel;
import com.oocl.constant.ProtocolType;
import com.oocl.pojo.Protocol;
import com.oocl.util.LogUtil;
import com.oocl.util.ResourceReleaser;

@SuppressWarnings("serial")
public class Register extends JFrame implements ActionListener {
	
	private JLabel titleJLabel;
	private JTextField nameTf;
	private JPasswordField  psdPf;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JLabel usernameLabel;//login
	private JLabel passwordLabel;//password
	private JLabel tips;
	
	private String username;
	
	public Register() {
		this.setSize(400, 260);
		this.setResizable(false);//窗体是否可以调整大小
		this.setLocationRelativeTo(null);//位于屏幕中央
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
		init();
		addEvent();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(null);
		nameTf=new JTextField();
		psdPf=new JPasswordField();
		loginBtn=new JButton("login");
		cancelBtn=new JButton("register");
		titleJLabel=new JLabel("用户注册");
		usernameLabel=new JLabel("username:");
		passwordLabel=new JLabel("password:");
		tips=new JLabel();
		
		titleJLabel.setBounds(160,2,230,50);
		titleJLabel.setFont(new Font("宋体", Font.BOLD, 20));
		nameTf.setBounds(120, 50, 230, 40);
		psdPf.setBounds(120, 100, 230, 40);
		loginBtn.setBounds(120, 160, 90, 40);
		cancelBtn.setBounds(260, 160, 90, 40);
		usernameLabel.setBounds(40,50, 70, 40);
		passwordLabel.setBounds(40,100, 70, 40);
		tips.setBounds(120, 130, 230, 40);
		
		this.add(titleJLabel);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(usernameLabel);
		this.add(passwordLabel);
		this.add(tips);
	}
	private void addEvent(){
		loginBtn.addActionListener(this);
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
//		if("".equals(nameTf.getText())){
//			tips.setText("username can not be empry!");
//		}else {
//			username = nameTf.getText();
//			Socket socket = null;
//			ObjectInputStream in = null;
//			ObjectOutputStream out = null;
//			try {
//				//10.222.29.201
//				socket = new Socket("localhost", 8888);
////				out = new ObjectOutputStream(socket.getOutputStream());
////				in = new ObjectInputStream(socket.getInputStream());
//				Protocol protocol = new Protocol();
//				protocol.setType(ProtocolType.LOGIN);
//				protocol.setFrom(username);
//				protocol.setTime(new Date());
//				out.writeObject(protocol);
//				out.flush();
//				
//				Protocol pro1 = (Protocol) in.readObject();
//				if(pro1.getType()==ProtocolType.LOGIN_SUCCESS){
//					LogUtil.log("login secess", LogLevel.INFO);
////					new ChatFrame(socket, in, out, username,pro1.getOnlineUsers());
//					Register.this.setEnabled(false);
//					Register.this.setVisible(false);
//					return;
//				}else {
//					LogUtil.log("login fail", LogLevel.INFO);
//					tips.setText("usern "+pro1.getContent());
//					ResourceReleaser.release(out, in, socket);
//				}
//			} catch (UnknownHostException e1) {
//				LogUtil.log("UnknownHostException!"+e1.getMessage(), LogLevel.ERROR);
//				JOptionPane.showMessageDialog(null, "找不到服务器连接", "Tip", JOptionPane.ERROR_MESSAGE);
//				ResourceReleaser.release(out, in, socket);
//			} catch (IOException e1) {
//				LogUtil.log("IOException!"+e1.getMessage(), LogLevel.ERROR);
//				JOptionPane.showMessageDialog(null, "找不到服务器连接", "Tip", JOptionPane.ERROR_MESSAGE);
//				ResourceReleaser.release(out, in, socket);
//			} catch (ClassNotFoundException e1) {
//				LogUtil.log("ClassNotFoundException!"+e1.getMessage(), LogLevel.ERROR);
//				JOptionPane.showMessageDialog(null, "找不到服务器连接", "Tip", JOptionPane.ERROR_MESSAGE);
//				ResourceReleaser.release(out, in, socket);
//			}
//		}
//		
	}
	public static void main(String[] args) {
		new Register();
	}
	
}
